/**
 * 节目单
 * 
 * board_2018/programme2019
 * 
 * @author Zdream
 * @date 2023-01-29
 * @since Aurora 0.5.0
 */

function create(context, key) {
  switch (key) {
    // main: 创建 / 重排序
    case 'main': return createMain(context);
    default: return null;
  }
}

function createMain(context) {
  return {
    template : context.template,
    data : function () {
      return {
        status : null,

        // 要传的值, 已存在的
        // {prefix:string, title:string, subtitles:string[], suffixType:string, suffix:string[], id:number, dirty:boolean}
        programmes : [],

        // 新建的
        nPrefix : '',
        nTitle : '',
        nSubtitles : ['', ''],
        nSuffixType : 'normal',
        nSuffix : [],
      }
    },
    props : {
      sid : String,
    },
    methods : {
      onScreenUpdated : function () {
        this.status = context.getStatus(this.sid);
        if (this.status == null || this.status.data == null) {
          return;
        }
        const data = this.status.data;
        this.programmes = data.order.map(id => {
          let cache = this.programmes.find(p => p.id == id);
          if (cache == null) {
            cache = data.programmes.find(p => p.id == id);
            cache.dirty = false;
          }
          return cache;
        });
      },

      // 事件驱动
      btnCreateSubmit : function () {
        this.sendCreate();
        this.nPrefix = '';
        this.nTitle = '';
        this.nSubtitles = ['', ''];
        this.nSuffixType = 'normal';
        this.nSuffix = [];
      },
      btnUpdateSubmit : function () {
        this.sendUpdate();
      },
      btnRefresh : function () {
        this.programmes = [];
        this.onScreenUpdated();
      },
      btnSwitch : function (index1, index2) {
        let p1 = this.programmes[index1];
        let p2 = this.programmes[index2];
        this.programmes[index1] = p2;
        this.programmes[index2] = p1;
        this.sendSort();
      },
      btnRemove : function (index) {
        this.programmes.splice(index, 1);
        this.sendSort();
      },

      // 发送
      sendCreate : function () {
        context.sendCommand(this.sid, 'create', {
          prefix: this.nPrefix,
          title: this.nTitle,
          subtitles: this.nSubtitles,
          suffixType: this.nSuffixType,
          suffix: this.nSuffix,
        });
      },
      sendSort : function () {
        context.sendCommand(this.sid, 'sort', {
          order: this.programmes.map(p => p.id),
        });
      },
      sendUpdate : function () {
        this.programmes.forEach(p => {
          if (!p.dirty) {
            return;
          }
          context.sendCommand(this.sid, 'update', {
            prefix: p.prefix,
            title: p.title,
            subtitles: p.subtitles,
            suffixType: p.suffixType,
            suffix: p.suffix,
            id: p.id,
          });
          p.dirty = false;
        });
      },
    },
    mounted : function () {
      this.onScreenUpdated(); // 更新 status
      context.getBus().$on(context.events.KEY_SCREEN_REFRESH, this.onScreenUpdated);
      context.getBus().$on(context.events.KEY_SCREEN_CREATED, this.onScreenUpdated);
      context.getBus().$on(context.events.KEY_SCREEN_UPDATED, this.onScreenUpdated);
    },
    destroyed : function () {
      context.getBus().$off(context.events.KEY_SCREEN_REFRESH, this.onScreenUpdated);
      context.getBus().$off(context.events.KEY_SCREEN_CREATED, this.onScreenUpdated);
      context.getBus().$off(context.events.KEY_SCREEN_UPDATED, this.onScreenUpdated);
    }
  };
}

export default {
  factory : create,
  tabs : [],
};

